<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Admin\ComController;
use DB;

class ComController extends Controller
{
    
    // 获取 单个商品 详情
    public function comDetail( $com_id )
    {
		// 商品主表信息
	    $com_info = ComController::getComById( $com_id );
		// 商品图片表信息
    	$pics_info = ComController::getComPicById( $com_id );
    	return view( 'Home.sku.detail', [ 'com_info' => $com_info, 'pics_info' => $pics_info ] );
    }
	
	// 前台 获取 商品列表 
	public function getComs( $category_id='',Request $request )
	{
		if( $category_id!='' )
		{
			// 获取所有分类
			$all_category = DB::table('category')
					-> select('id')
					-> where('path', 'like', '%-'.$category_id.'-%')
					-> get();
			$category = array( $category_id );
			foreach( $all_category as $key=>$category_info )
			{
				$category[] = $category_info->id;
			}
			// 获取 所有商品
			$all_com = DB::table('com as c')
						->select('c.id','c.name','cp.pic_url','c.price','c.description','c.stock','c.status','c.category_id')
						->leftJoin('com_pic as cp','c.id','=','cp.sku')
						->whereIn('c.category_id',$category)
						// 主图
						->where('cp.type','=',1 )
						->orderBy('c.id')
						->paginate(5);
			// 有分类
			return view( 'Home.sku.list', [ 'coms' => $all_com ] );
		}
		
		// 搜索内容 没有默认 空
		$keyword = $request->input('keyword',"");
		if( empty($keyword) )
		{
			$one = 'c.id';
			$two = '!=';
			$three = '0';
		}
		else
		{
			$one = 'c.name';
			$two = 'like';
			$three = "%".$keyword."%";
		}
		
		// 获取 所有商品
		$all_com = DB::table('com as c')
						->select('c.id','c.name','cp.pic_url','c.price','c.description','c.stock','c.status')
						->leftJoin('com_pic as cp','c.id','=','cp.sku')
						->where( $one,$two,$three )
						// 主图
						->where('cp.type','=',1 )
						->orderBy('c.id')
						->paginate(5);
			
		// 有搜索
		return view( 'Home.sku.list', [ 'coms' => $all_com,'request'=>$keyword ] );
		
	}

	// 指定商品的 库存信息
	public static function getStock( $com_id )
	{
		$stock_info = DB::table('com')
						->select( 'stock' )
						->where( 'id','=',$com_id )
						->first();
		return $stock_info;
	}
	
	
	// 商品销量排行榜
	public function comOrder()
	{
		// 对订单详情 统计商品销量
		$sales = DB::table('order_detail as odr')
                -> select( 'odr.sku',DB::raw('SUM(odr.cnt) as num') )
                -> groupBy('odr.sku')
				// 按 销量 排行
				-> orderBy('num','desc')
				-> orderBy('id','desc')
				// 为了效率 取前20名
				-> take(20)
                -> get();
		// 商品 不为空
		if( !empty($sales) )
		{
			$com_arr = array();
			foreach( $sales as $key=>$sale_info )
			{
				// 获取 商品ID 集合
				$com_arr[$sale_info->sku] = $sale_info->sku;
			}
			// 批量 获取商品 详情
			$coms = DB::table('com as c')
						->select('c.id','c.name','cp.pic_url')
						->leftJoin('com_pic as cp','c.id','=','cp.sku')
						->whereIn( 'c.id',$com_arr )
						// 主图
						->where('cp.type','=',1 )
						->get();
			$final = array();
			foreach( $sales as $key=>$sale_info )
			{
				foreach( $coms as $k=>$com_info )
				{
					// 获取 商品ID 集合
					if( $sale_info->sku == $com_info->id )
					{
						// $all_com[$k]['num'] = $com_info->num;
						$final[$key]['id']= $sale_info->sku;
						$final[$key]['name']= $com_info->name;
						$final[$key]['num']= $sale_info->num;
						$final[$key]['pic_url']= $com_info->pic_url;
					}
				}
			}
		}else
		{
			$final = array();
		}
		// 页面
		return view( 'Home.sku.orderBy',['coms'=>$final] );
	}



}
